package edu.upc.galaxy.controller;

import edu.upc.galaxy.entity.Credential;
import edu.upc.galaxy.entity.Persona;
import edu.upc.galaxy.service.PersonaService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Alvaro
 */
@Controller("usuarioController")
public class PersonaController {

    @Autowired
    private PersonaService usuarioService;
    private static Logger log = LoggerFactory.getLogger(PersonaController.class);

    @RequestMapping("/personas/index")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("personas/index");
        List<Persona> personas = usuarioService.buscarTodos();
        mav.addObject("personas", personas);
        return mav;
    }
    
    @RequestMapping(value = "/personas/new", method = RequestMethod.GET)
    public ModelAndView newUsuario() {
        ModelAndView mav = new ModelAndView("personas/new");
        Persona persona = new Persona();
        mav.getModelMap().put("persona", persona);
        return mav;
    }

    @RequestMapping(value = "/personas/new", method = RequestMethod.POST)
    public String createUsuario(@ModelAttribute("persona")Persona persona, SessionStatus status) {    
        usuarioService.insertar(persona);
        status.setComplete();

        return "redirect:/pages/personas/index";
        
    }
    
    @RequestMapping(value = "/personas/edit", method = RequestMethod.GET)
    public ModelAndView editUsuario(@RequestParam("id")Integer id) {    
        ModelAndView mav = new ModelAndView("personas/edit");
        Persona persona = usuarioService.buscar(id);
        mav.getModelMap().put("persona", persona);
        return mav;        
    }
    
    @RequestMapping(value="/personas/edit", method=RequestMethod.POST)
    public String update(@ModelAttribute("persona") Persona persona, SessionStatus status) {
        usuarioService.actualizar(persona);
        status.setComplete();
        return "redirect:/pages/personas/index";
    }
    
    @RequestMapping("/personas/delete")
    public ModelAndView delete(@RequestParam("id")Integer id)
    {
        ModelAndView mav = new ModelAndView("redirect:/pages/personas/index");
        Persona persona = usuarioService.buscar(id);
        usuarioService.eliminar(persona);
        return mav;
    }
    
    @RequestMapping("/personas/login")
    public ModelAndView login() {
        ModelAndView mav = new ModelAndView("personas/login");
        Credential credential = new Credential();
        mav.getModelMap().put("credential", credential);
        return mav;
    }    
    
    @RequestMapping(value = "/personas/autenticar", method=RequestMethod.POST)
    public String autenticar(@ModelAttribute("credential") Credential credential,HttpServletRequest request) {
        if (usuarioService.autenticar(credential.getCorreo(), credential.getPassword())) {
            request.getSession().setAttribute("credential",credential);
            return "redirect:/pages/personas/index";
        }
        return "redirect:/pages/personas/login";
    }
    
    @RequestMapping("/personas/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return "redirect:/pages/personas/login";
    }        
    
    
}
